package com.nfcuttings.presuvinil;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.math.BigDecimal;

import android.util.Log;

public class Utils {

	public static Double getRoundedValue(Double numero, int cantidadDigitos){
		if(numero==null)
			numero = Double.valueOf(0);

		BigDecimal bd = new BigDecimal(numero);
		bd = bd.setScale(cantidadDigitos, BigDecimal.ROUND_HALF_UP);
		return bd.doubleValue();
	}

	public static Double obtenerValorRedondeado(Double numero){
		return getRoundedValue(numero,2);
	}

	public static byte[] pasarvalorabytes(Object valor){
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutput out = null;
		byte[] yourBytes = null;
		try {
			out = new ObjectOutputStream(bos);   
			out.writeObject(valor);
			yourBytes = bos.toByteArray();
		} catch (Exception e) {
			Log.e("Utils pasarvalorabytes ERROR>", e.getLocalizedMessage(), e);
		} finally {
			try {
				out.close();
				bos.close();
			} catch (IOException e) {
				Log.e("Utils obtenerObjetoDesdeBytes ERROR>", e.getLocalizedMessage(), e);
			}
		}
		return yourBytes;
	}

	public static Object obtenerObjetoDesdeBytes(byte[] yourBytes){
		ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes);
		ObjectInput in = null;
		Object o = null;
		try {
			in = new ObjectInputStream(bis);
			o = in.readObject(); 
		} catch (Exception e) {
			Log.e("Utils obtenerObjetoDesdeBytes ERROR>", e.getLocalizedMessage(), e);
		} finally {
			try {
				bis.close();
				in.close();
			} catch (IOException e) {
				Log.e("Utils obtenerObjetoDesdeBytes ERROR>", e.getLocalizedMessage(), e);
			}
		}
		return o;
	}

}
